home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '90 / MacHack 90 Contest Entries / What day is it?.ƒ / Installer project / installer.c next >
Encoding:
C/C++ Source or Header  |  1990-06-15  |  10.4 KB  |  562 lines  |  [TEXT/KAHL]

  1.  
  2. #include "SerialDvr.h"
  3. #include "installer.h"
  4.  
  5. /******** MAC global vars **********/
  6.     Rect                 screen;
  7.     int                    half_h,half_v;
  8.     EventRecord theEvent;
  9.     MenuHandle    DeskMenu,FileMenu;
  10.     Str255            theAcc;
  11.     WindowPtr        theWindow;
  12.  
  13. /***********************************  test code here  ********************************/
  14. DoIt()
  15.     {
  16.     SysEnvRec    system;
  17.     int                result;
  18.     int                saveResFile;
  19.     DialogPtr    dd;
  20.     int                done;
  21.     int                itemHit;
  22.     int                t;
  23.     Handle        h;
  24.     Rect            r;
  25.     
  26.  
  27.     if ( isInstalled() )
  28.         {
  29.     
  30.         dd = GetNewDialog(129,nil,(WindowPtr)-1);
  31.         if ( dd != nil )
  32.             {
  33.             ParamText("\pINSTALLED","\pREMOVE","\p","\p");
  34.             GetDItem(dd,1,&t,&h,&r);
  35.             SetCTitle(h,"\pREMOVE");
  36.             installOutline(dd,5);
  37.             done = NO;
  38.             while (!done)
  39.                 {
  40.                 ModalDialog(nil,&itemHit);
  41.                 switch(itemHit)
  42.                     {
  43.                     case 1:
  44.                         doRemove();
  45.                         done = YES;
  46.                         break;
  47.                     case 2:
  48.                         done = YES;
  49.                         break;
  50.                     }
  51.                 }
  52.             DisposDialog(dd);
  53.             }
  54.         else
  55.             SysBeep(1);
  56.         }
  57.     else
  58.         {
  59.         dd = GetNewDialog(129,nil,(WindowPtr)-1);
  60.         if ( dd != nil )
  61.             {
  62.             ParamText("\pNOT installed","\pINSTALL","\p","\p");
  63.             GetDItem(dd,1,&t,&h,&r);
  64.             SetCTitle(h,"\pINSTALL");
  65.             installOutline(dd,5);
  66.             done = NO;
  67.             while (!done)
  68.                 {
  69.                 ModalDialog(nil,&itemHit);
  70.                 switch(itemHit)
  71.                     {
  72.                     case 1:
  73.                         doInstall();
  74.                         done = YES;
  75.                         break;
  76.                     case 2:
  77.                         done = YES;
  78.                         break;
  79.                     }
  80.                 }
  81.             DisposDialog(dd);
  82.             }
  83.         else
  84.             SysBeep(1);
  85.         }
  86.     }
  87.  
  88. doInstall()
  89.     {
  90.     int                t;
  91.     Handle        h;
  92.     Rect            r;
  93.     DialogPtr dd;
  94.     OSErr            err = noErr;
  95.     int                done;
  96.     int                itemHit;
  97.     long            finalTicks;
  98.     int                saveResFile;
  99.     Handle        hh = nil;
  100.     Handle        MYhh = nil;
  101.     Handle        MYrov = nil;
  102.     Handle        MYrovq = nil;
  103.  
  104.     dd = GetNewDialog(130,nil,(WindowPtr)-1);
  105.     ParamText("\pInstalling the WDEF patch.","\p","\p","\p");
  106.     GetDItem(dd,1,&t,&h,&r);
  107.     HideControl(h);
  108.     DrawDialog(dd);
  109.     
  110.     /*get my patch from the program*/
  111.     MYhh = GetResource('WDEF',68);
  112.     if ( *MYhh == nil )
  113.         LoadResource(MYhh);
  114.     if ( MYhh == nil || *MYhh == nil)
  115.         {
  116.         SysBeep(1);SysBeep(1);SysBeep(1);return(resNotFound);
  117.         }
  118.     DetachResource(MYhh);
  119.     HLock(MYhh);
  120.  
  121.     MYrov = GetResource('ROv#',0);
  122.         if ( *MYrov == nil )
  123.         LoadResource(MYrov);
  124.     if ( MYrov == nil || *MYrov == nil )
  125.         {
  126.         SysBeep(1);SysBeep(1);SysBeep(1);return(resNotFound);
  127.         }
  128.     DetachResource(MYrov);
  129.     HLock(MYrov);
  130.  
  131.     /*remember our res file number and set it to the system*/
  132.     saveResFile = CurResFile();
  133.     UseResFile(0);
  134.  
  135.  
  136.     /*kill any existing 69 in the system file*/
  137.     hh = GetResource('WDEF',69);
  138.     if ( hh != nil )
  139.         {
  140.         RmveResource(hh);
  141.         err = ResError();
  142.         UpdateResFile(0);
  143.         err = ResError();
  144.         }
  145.  
  146.     /*update rov#*/
  147.     hh = GetResource('ROv#',376);
  148.     if ( hh != nil )
  149.         {
  150.         RmveResource(hh);
  151.         err = ResError();
  152.         UpdateResFile(0);
  153.         err = ResError();
  154.         }
  155.     AddResource(MYrov,'ROv#',376,"\p][cx");
  156.     err = ResError();
  157.     UpdateResFile(0);
  158.     err = ResError();
  159.     
  160.     MYrovq = NewHandle(8);
  161.     AddResource(MYrovq,'ROv?',376,"\pwhat time is it hack");
  162.     err = ResError();
  163.     UpdateResFile(0);
  164.     err = ResError();
  165.     
  166.     /*get the system's wdef#0 and write it to #69*/
  167.     hh = GetResource('WDEF',0);
  168.     if ( *hh == nil )
  169.         LoadResource(hh);
  170.     if ( hh == nil || *hh == nil )
  171.         {
  172.         SysBeep(1);SysBeep(1);SysBeep(1);return(resNotFound);
  173.         }
  174.     HLock(hh);
  175.     DetachResource(hh);
  176.     AddResource(hh,'WDEF',69,"\pOriginal WDEF #0");
  177.     err = ResError();
  178.     if ( err != noErr )
  179.         {
  180.         SysBeep(1);SysBeep(1);SysBeep(1);return(err);
  181.         }
  182.     WriteResource(hh);
  183.     ReleaseResource(hh);
  184.     
  185.     
  186.     /*get the system's wdef#0 again and remove it*/
  187.     hh = GetResource('WDEF',0);
  188.     if ( hh == nil )
  189.         {
  190.         SysBeep(1);SysBeep(1);SysBeep(1);return(resNotFound);
  191.         }
  192.     RmveResource(hh);
  193.     UpdateResFile(0);
  194.     
  195.     
  196.     /*add my resource in place of the old #0*/
  197.     AddResource(MYhh,'WDEF',0,"\pEric's WDEF");
  198.     WriteResource(MYhh);
  199.     ReleaseResource(MYhh);
  200.  
  201.  
  202.     /*point the current res file to us*/
  203.     UseResFile(saveResFile);    
  204.     
  205.     
  206.     /*tell'em it's done*/
  207.     if ( err == noErr ) 
  208.         ParamText("\pWDEF patch sucessfully installed.","\p","\p","\p");
  209.     else
  210.         ParamText("\pThere was an error installing the WDEF patch.","\p","\p","\p");
  211.  
  212.     installOutline(dd,4);    
  213.     GetDItem(dd,1,&t,&h,&r);
  214.     ShowControl(h);
  215.     
  216.     done = NO;
  217.     while (!done)
  218.         {
  219.         ModalDialog(nil,&itemHit);
  220.         switch(itemHit)
  221.             {
  222.             case 1:
  223.                 done = YES;
  224.                 break;
  225.             }
  226.         }
  227.     DisposDialog(dd);
  228.     }
  229.  
  230. doRemove()
  231.     {
  232.     int                t;
  233.     Handle        h;
  234.     Rect            r;
  235.     DialogPtr dd;
  236.     OSErr            err = noErr;
  237.     int                done;
  238.     int                itemHit;
  239.     long            finalTicks;
  240.     int                saveResFile;
  241.     Handle        hh = nil;
  242.     Handle        MYhh = nil;
  243.     Handle        MYrov = nil;
  244.     Handle        MYtest = nil;
  245.  
  246.     dd = GetNewDialog(130,nil,(WindowPtr)-1);
  247.     ParamText("\pRemoving the WDEF patch.","\p","\p","\p");
  248.     GetDItem(dd,1,&t,&h,&r);
  249.     HideControl(h);
  250.     DrawDialog(dd);
  251.     
  252.     MYrov = GetResource('ROv#',1);
  253.     if ( *MYrov == nil )
  254.         LoadResource(MYrov);
  255.     if ( MYrov == nil || *MYrov == nil )
  256.         {
  257.         SysBeep(1);SysBeep(1);SysBeep(1);return(resNotFound);
  258.         }
  259.     DetachResource(MYrov);
  260.     HLock(MYrov);
  261.  
  262.     saveResFile = CurResFile();
  263.     UseResFile(0);
  264.     
  265.     MYtest = GetResource('ROv?',376);
  266.     if ( MYtest == nil )
  267.         {
  268.         MYhh = GetResource('WDEF',69);
  269.         if ( *MYhh == nil )
  270.             LoadResource(MYhh);
  271.         if ( MYhh == nil || *MYhh == nil)
  272.             return(resNotFound);
  273.         DetachResource(MYhh);
  274.         HLock(MYhh);
  275.         
  276.         hh = GetResource('WDEF',0);
  277.         if ( *hh == nil )
  278.             LoadResource(hh);
  279.         if ( hh == nil || *hh == nil )
  280.             return(resNotFound);
  281.         HLock(hh);
  282.     
  283.         RmveResource(hh);
  284.         AddResource(MYhh,'WDEF',0,"\p");
  285.     
  286.         hh = GetResource('WDEF',69);
  287.         HLock(hh);
  288.         RmveResource(hh);
  289.     
  290.         WriteResource(MYhh);
  291.         ReleaseResource(MYhh);
  292.         }
  293.     else
  294.         {
  295.         /*update rov#*/
  296.         RmveResource(MYtest);
  297.         UpdateResFile(0);
  298.         hh = GetResource('ROv#',376);
  299.         if ( hh != nil )
  300.             {
  301.             RmveResource(hh);
  302.             err = ResError();
  303.             UpdateResFile(0);
  304.             err = ResError();
  305.             }
  306.         AddResource(MYrov,'ROv#',376,"\p");
  307.         WriteResource(MYrov);
  308.         err = ResError();
  309.         UpdateResFile(0);
  310.         err = ResError();
  311.         }
  312.         
  313.     UseResFile(saveResFile);    
  314.     
  315.     if ( err == noErr ) 
  316.         ParamText("\pWDEF patch sucessfully removed.","\p","\p","\p");
  317.     else
  318.         ParamText("\pThere was an error removing the WDEF patch.","\p","\p","\p");
  319.  
  320.     installOutline(dd,4);    
  321.     GetDItem(dd,1,&t,&h,&r);
  322.     ShowControl(h);
  323.     
  324.     done = NO;
  325.     while (!done)
  326.         {
  327.         ModalDialog(nil,&itemHit);
  328.         switch(itemHit)
  329.             {
  330.             case 1:
  331.                 done = YES;
  332.                 break;
  333.             }
  334.         }
  335.     DisposDialog(dd);
  336.     }
  337. int isInstalled()
  338.     {
  339.     SysEnvRec    system;
  340.     int                result;
  341.     int                saveResFile;
  342.     Handle        hh = nil;
  343.     
  344.     saveResFile = CurResFile();
  345.     UseResFile(0);
  346.  
  347.     if ( hh == nil )
  348.         hh = GetResource('WDEF',0);
  349.  
  350.     if ( *hh == nil )
  351.         LoadResource(hh);
  352.     
  353.     HLock(hh);
  354.     if ( isMyString(hh) )
  355.         result = TRUE;
  356.     else
  357.         result = FALSE;
  358.     
  359.     UseResFile(saveResFile);    
  360.     return(result);
  361.     }
  362.  
  363. int isMyString(hh)
  364.  Handle hh;
  365.     {
  366.     if (*(long *)&(*hh)[8] == 'Eric')
  367.         return(TRUE);
  368.     else
  369.         return(FALSE);
  370.     }
  371.     
  372. /***********************************  dumb shell here  ******************************/
  373.  
  374.  
  375. /*This is what runs when the Bomb dialog box comes up, this Hilites the resume button*/
  376. reStartProc()
  377.     {
  378.    ExitToShell();
  379.     }
  380.  
  381. Inits()
  382.     {
  383.     MaxApplZone ();
  384.     MoreMasters ();
  385.     InitGraf(&thePort);
  386.     InitFonts();
  387.     InitWindows();
  388.     InitMenus();
  389.     InitCursor();
  390.     InitDialogs(reStartProc);
  391.    
  392.   FlushEvents(everyEvent, nil);
  393.    
  394.   SetRect(&screen,
  395.       screenBits.bounds.left, 
  396.       screenBits.bounds.top, 
  397.       screenBits.bounds.right, 
  398.       screenBits.bounds.bottom);
  399.    
  400.   /*center of the screen*/
  401.   half_h = (screenBits.bounds.right  / 2);
  402.   half_v = (screenBits.bounds.bottom / 2);
  403.     }
  404.  
  405. SetUpMenus()
  406.     {    
  407.     long    items;
  408.     
  409.     /*  Desk Accessory Menu*/
  410.     DeskMenu = NewMenu (Desk_ID,"\p\24");
  411.     AppendMenu (DeskMenu,"\pAbout Eric's work shell...;(-");
  412.     AddResMenu (DeskMenu, 'DRVR');
  413.     items = CountMItems(DeskMenu);
  414.     InsertMenu (DeskMenu, 0);
  415.     
  416.     /*  file menu  */
  417.     FileMenu = NewMenu (File_ID,"\pFile");
  418.     AppendMenu (FileMenu,"\pDo it./G;(-;Quit/Q");
  419.     InsertMenu (FileMenu, 0);
  420.     
  421.     DrawMenuBar();
  422.     }
  423.  
  424. OpenWindow()
  425.     {
  426.     Point    tempPt;
  427.     int        Offset;
  428.     Rect    bounds;
  429.     SetRect(&bounds,100,100,200,200);
  430.     
  431.     theWindow = GetNewWindow(1000,nil, (WindowPtr)-1);
  432.     
  433.     SetPort(theWindow);
  434.     tempPt.v = theWindow->portRect.top;
  435.     tempPt.h = theWindow->portRect.left;
  436.     LocalToGlobal(&tempPt);
  437.     Offset = ((half_h*2) - (theWindow->portRect.right-theWindow->portRect.left))/2;
  438.     MoveWindow(theWindow,Offset,tempPt.v,FALSE);
  439.     ShowWindow(theWindow);
  440.     }
  441.  
  442.  
  443. main()
  444.     {
  445.     WindowPtr whichWindow;
  446.     short            windowcode;
  447.     char            c;
  448.     WindowPtr    savePort;
  449.     int                stillInGoAway;
  450.     Rect            tempRect;
  451.     long            Result;
  452.     Point            myPt;
  453.     
  454.     Inits();
  455.     SetUpMenus();
  456.     /*OpenWindow();*/
  457.     
  458.     while (TRUE)
  459.         {
  460.         if (GetNextEvent(everyEvent,&theEvent))
  461.             { 
  462.             SystemTask();
  463.             switch (theEvent.what)
  464.                 { 
  465.                 case mouseDown: 
  466.                     windowcode=FindWindow(Pt2ROM(theEvent.where),&whichWindow);
  467.                     switch (windowcode)
  468.                         {
  469.                         case inMenuBar:
  470.                             DoMenu(MenuSelect(Pt2ROM(theEvent.where)));
  471.                             break;
  472.                         case inSysWindow:
  473.                             SystemClick(&theEvent,whichWindow);
  474.                             break;
  475.                         case inDrag: 
  476.                         if (whichWindow != nil)
  477.                             DragWindow(whichWindow,theEvent.where,&screen);
  478.                             break;
  479.                         case inGoAway:
  480.                          stillInGoAway = TrackGoAway(whichWindow,Pt2ROM(theEvent.where));
  481.                          if(stillInGoAway == TRUE)
  482.                              ExitToShell();
  483.                          break;
  484.                      case inGrow:
  485.                             SetRect(&tempRect,50,50,screen.right,screen.bottom);
  486.                             Result = GrowWindow(whichWindow, theEvent.where, &tempRect);
  487.                             if ( Result != nil )
  488.                                 SizeWindow(whichWindow, LoWord(Result), HiWord(Result), TRUE);
  489.                             break;
  490.                         case inZoomIn:
  491.                         case inZoomOut:
  492.                             windowcode = FindWindow(theEvent.where,&whichWindow);
  493.                             if (whichWindow != nil)
  494.                                 {
  495.                                 GetPort(&savePort);
  496.                                 SetPort(whichWindow);
  497.      
  498.                                 myPt = theEvent.where;
  499.                                 GlobalToLocal(&myPt);
  500.      
  501.                                 if (TrackBox(whichWindow,myPt, windowcode))
  502.                                     ZoomWindow(whichWindow, windowcode, TRUE);
  503.                                 }
  504.                             break;
  505.                         }
  506.                     break;
  507.                 case keyDown:
  508.                     if (theEvent.modifiers & cmdKey)
  509.                         {
  510.                         c = theEvent.message & charCodeMask;  
  511.                         DoMenu(MenuKey(c));
  512.                         }
  513.                     break;
  514.                     
  515.                 }  /*switch*/
  516.             }  /*if(GNE)*/
  517.         } /*while(true)*/
  518.     } /* end of main() */
  519.  
  520.  
  521. DoMenu(menuResult)
  522.  long    menuResult;
  523.     {
  524.     short            menuID, itemNumber,i;
  525.     Str255        theItem;
  526.     
  527.     menuID         = HiWord (menuResult);
  528.     itemNumber    = LoWord (menuResult);
  529.     
  530.     switch (menuID) 
  531.         {
  532.         case Desk_ID:
  533.             switch(itemNumber)
  534.                 {
  535.                 case 1:
  536.                     doAbout();
  537.                     break;
  538.                 default:
  539.                     GetItem(DeskMenu,itemNumber, &theAcc);
  540.                     OpenDeskAcc(&theAcc);
  541.                     break;
  542.                 }
  543.             break;
  544.         case File_ID:
  545.             switch (itemNumber)
  546.                 {
  547.                 case fm_doit:
  548.                     DoIt();
  549.                     break;
  550.                 case fm_quit:
  551.                     ExitToShell();
  552.                     break;
  553.                 }
  554.             break;        
  555.         }
  556.     HiliteMenu(0);
  557.     }
  558.  
  559. doAbout()
  560.     {
  561.     int r = Alert(1000,0L);
  562.     }